const app = getApp()
const request = require('../../../utils/https.js')
import util from '../../../utils/util.js';
var fromUser, userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    numbers: 0,
    objectFit: "contain",
    collection: false,
    play: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    console.log('=====>', options)
    userId = wx.getStorageSync("userId");
    let mateId = options.mateId;
    let kinds = options.kinds;
    console.log('kinds===>', kinds)
    //kinds 0 图片 1视频
    let title;
    if (kinds == 0) {
      title = '素材详情'
    } else if (kinds == 1) {
      let title = ''
    }

    request._get(getApp().Api_url + 'mate/cateDetail', {
      mateId: mateId,
      userId: userId
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        var dataDeta = res.data.data;
        that.setData({
          kinds: kinds,
          title: title,
          mateId: mateId,
          dataDeta: dataDeta,
          collection: dataDeta.store, //是否收藏
          downloadNum: dataDeta.downloadNum, //下载次数
          storeNum: dataDeta.storeNum, //收藏次数
          carouselsImg: dataDeta.carouselsImg
        })
      }

    }, function () {})
    //设备显示区域的宽高比
    wx.getSystemInfo({
      success: function (res) {

        console.log('设备品牌==>', res.brand)
        console.log('设备型号===>', res.model)
        console.log('客户端平台===>', res.platform)
        console.log('操作系统及版本===>', res.system)

        let win_width = res.windowWidth;
        let win_height = res.windowHeight;
        let height = 750 / (win_width / win_height)
        that.setData({
          height: height,
          win_width: win_width,
          win_height: win_height,
          model: res.model,
          brand: res.brand
        })
      }
    })

  },
  //文本复制
  Clipboar() {
    console.log('复制')
    const that = this;
    wx.setClipboardData({
      data: that.data.dataDeta.content,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '复制成功',
            });
          }
        })
      }
    })
  },
  onCurrent(e) {
    const that = this;
    that.setData({
      numbers: e.detail.current
    })
  },

  //查看详情页
  goOrder(e) {
    const that = this;

    let userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      // wx.reLaunch({
      //   url: '/pages/login/login?path=/pages/list/list'
      // })
      // return;
      userId = "";
    }
    let goodsId = e.currentTarget.dataset.goodsid;
    let fast = e.currentTarget.dataset.fast;
    console.log('fast类型==>', typeof (fast))
    /*     let userId = wx.getStorageSync("userId");
         if (userId == '' || userId == null || userId == 'undefined') {
          wx.navigateTo({
            url: '/pages/login/login',
          })
          return;
        } */
    wx.navigateTo({
      url: '/pages/gift_details/gift_details?goodsId=' + goodsId + '&show=' + 0 + '&userId=' + fromUser + '&fast=' + fast
    })
  },





  //收藏
  onCollection(e) {
    const that = this;
    let mateId = that.data.mateId;
    let collection = e.currentTarget.dataset.collection;
    request._get(getApp().Api_url + 'mate/user/store', {
      mateId: mateId,
      userId: userId
    }, function (res) {
      if (res.data.errMsg == 'OK') {}

    }, function () {})

    if (collection) {
      console.log('收藏')
      that.setData({
        collection: collection,
        storeNum: that.data.storeNum + 1
      })
      wx.showToast({
        icon: 'none',
        title: '收藏',
      })
    } else {
      console.log('取消收藏')
      that.setData({
        collection: collection,
        storeNum: that.data.storeNum - 1
      })
      wx.showToast({
        icon: 'none',
        title: '取消收藏',
      })
    }

  },
  //下载文件
  onDownload() {
    const that = this;
    let mateId = that.data.mateId;
    request._get(getApp().Api_url + 'mate/download', {
      mateId: mateId,
      userId: userId
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        that.setData({
          downloadNum: that.data.downloadNum + 1
        })
      }
    }, function () {})
    wx.showLoading({
      title: '压缩中',
    })

    let carouselsImg = that.data.carouselsImg;
    carouselsImg.forEach((v) => {
      wx.downloadFile({
        url: v,
        success(res) {
          // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
          console.log('下载===>', res)
          if (res.statusCode == 200) {
            console.log('=res.tempFilePath==>', res.tempFilePath)
            var tempFilePath = res.tempFilePath;
            console.log('tempFilePath==>', tempFilePath)
            wx.hideLoading();

            //保存到本地
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success: function () {
                if (that.data.kinds == 1) { //视频
                  //视频保存到相册
                  wx.saveVideoToPhotosAlbum({
                    filePath: tempFilePath,
                    success(res) {
                      console.log(res.errMsg)

                      wx.showToast({
                        icon: 'none',
                        title: '保存成功',
                      })
                    }
                  })

                } else {
                  //图片保存到相册
                  wx.saveImageToPhotosAlbum({
                    filePath: tempFilePath,
                    success: function (res) {
                      console.log('res====>', res)

                      wx.showToast({
                        icon: 'none',
                        title: '保存成功',
                      })
                    },
                    fail: function (res) {
                      console.log('禁止保存到相册')
                      wx.showToast({
                        icon: 'none',
                        title: '保存失败',
                      })


                    }
                  })
                }

              }
            })

          }
        }
      })
    })





  },
  onClickLeft() {
    wx.showToast({
      title: '点击返回',
      icon: 'none'
    });
    wx.navigateBack({
      delta: 1
    })
  },
  //点击暂停/开始
  videoTap: function () {
    //获取video
    this.videoContext = wx.createVideoContext('myVideo')
    if (this.data.play) {
      //开始播放
      this.videoContext.play() //开始播放
      this.setData({
        play: false
      })
    } else {
      //当play==false 显示图片 暂停

      this.videoContext.pause() //暂停播放
      this.setData({
        play: true
      })
    }
  },
    //点击图放大
    imgTapOne: function (e) {
      const that = this;
      let index = e.currentTarget.dataset.index;
      let bannerList = that.data.carouselsImg;
      wx.previewImage({
        current: bannerList[index], // 当前显示图片的http链接
        urls: bannerList // 需要预览的图片http链接列表
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})